package top.yaofengqiao.springcloudsimple.common.util;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author yfq
 * @date 10:50
 * @description
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class PageInfo<T> {
    protected long total;
    protected long size;
    protected long current;
    protected List<T> records;

    public static <T> PageInfo<T> build(Page<T> page) {
        return PageInfo.<T>builder()
                .total(page.getTotal())
                .size(page.getSize())
                .current(page.getCurrent())
                .records(page.getRecords())
                .build();
    }

    public <K> PageInfo<K> convert(Function<T, K> convertor) {
        List<K> list = this.getRecords().stream().map(convertor).collect(Collectors.toList());

        return PageInfo.<K>builder()
                .total(this.getTotal())
                .size(this.getSize())
                .current(this.getCurrent())
                .records(list)
                .build();
    }

}
